Ext.define('qjtravel.controller.Product', {
    extend: 'Ext.app.Controller',

    config: {
        refs: {
            product: 'product',
            product_select: 'product #select',
            back: 'product #back',
            map: 'product #map',
            share: 'product #share',
            collect: 'product #collect',
            detail: 'ProductDetail',
            tel: 'ProductDetail #tel'
        },
        control: {
            product_select: {
                itemtap: function (list, index, element, record){
                    if (window.localStorage.getItem("productClick") == null) {
                        Ext.Viewport.mask({xtype: 'loadmask' });
                        window.localStorage.setItem("productClick", '1');
                       // localStorage.setItem('cpid', record.getData().cpid);
                        localStorage.setItem('isHome', 1);
                        var product_detail = this.getDetail();
                        if (product_detail == null) {
                            product_detail = Ext.create('qjtravel.view.ProductDetail');

                        }

                        //Ext.getCmp("detailCarousel").setItem([]);
                        var label = product_detail.query('label');
                        Ext.each(label, function (elem) {
                            elem.setData(record.data);
                        });
                        //set ProductDetail Carousel data
                        //console.log(Ext.getCmp("detailCarousel"))
                        var proDetailCarStore=Ext.getStore("ProductDetail");
                        proDetailCarStore.getProxy().setExtraParam("cpid", record.getData().cpid)
                        proDetailCarStore.load({
                            callback: function (data) {
                                var myAdPicItems = [];
                                Ext.each(data, function (productDetail) {
                                    var detail = productDetail.get('photos');
                                    Ext.each(detail, function (item) {
                                        myAdPicItems.push({
                                            xtype: 'panel',
                                            items: {
                                                html: "<img src='" + item.url + "' style='width:100%;height:100%;' / >"
                                            }
                                        });
                                    })
                                });
                                Ext.getCmp("detailCarousel").setItems(myAdPicItems);
                                Ext.getCmp("detailCarousel").setActiveItem(0);
                            }
                        })
                        console.log(record)
                        if(record.data.tel==" "&&record.data.address==" "&&record.data.href_url==""){
                            console.log("毛都没有...")
                           Ext.getCmp("fieldset").hide();
                        }else{
                            record.data.tel == ' ' ? Ext.getCmp('tel').hide() : product_detail.query('#tel')[0].setHtml(
                                '<div style="float: left;margin-right: 10px;"><img src="resources/images/tel2.png" height="20px" style=" vertical-align: middle"/></div>'+record.data.tel);
                            record.data.address == ' ' ? Ext.getCmp('address').hide() : product_detail.query('#address')[0].setHtml(
                                '<div style="float: left;margin-right: 10px;"><img src="resources/images/postion.png" height="20px" style=" vertical-align: middle"/></div>'+record.data.address);
                            //href_url跳转至携程
                            if(record.data.href_url == ""){
                                Ext.getCmp('gotoXC').hide();
                            }else{
                                Ext.getCmp("gotoXCUrl").setValue(record.data.href_url);
                                product_detail.query('#gotoXC')[0].setHtml(
                                    '<div style="float: left;margin-right: 10px;">' +
                                        '<img src="resources/images/1.png" height="20px" style=" vertical-align: middle"/>' +
                                        '</div>去携程网预订');
                            }
                        }
                        product_detail.query('#name')[0].setValue(record.data.name);
                        product_detail.query('#picurl')[0].setValue(record.data.picurl);
                        product_detail.query('#explain')[0].setValue(record.data.explain);
                        Ext.getCmp('toolbars').setTitle(record.data.name);
                        Ext.getCmp('callNumber').setValue(record.data.tel);
                        Ext.Viewport.animateActiveItem(product_detail, {type: 'slide', direction: 'left'});
                    }
                }
            }
        }
    },
    toArrayFn:function(obj){

            var array = [];
            // iterate backwards ensuring that length is an UInt32
            for (var i = obj.length >>> 0; i--;) {
                array[i] = obj[i];
            }
            return array;

    }
});